﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetFrame;
using NetFrame.auto;
using Server.tool;
using MoneyProtocol;
using System.Data;

namespace Server.logic.login
{
    public class SecurityAccountHandler : AbsOnceHandler, HandlerInterface
    {
        cache.IAccountCache ac = cache.CacheFactory.accountCache;
        model.Security_account sa = new model.Security_account();

        public void ClientClose(UserToken token, string error)
        {
            ExecutorPool.Instance.execute(
                     delegate ()
                     {
                         //Console.WriteLine("用户" + token.conn.ToString() + "关闭证券账号管理操作");
                     }
                     );
        }

        public void MessageReceive(UserToken token, SocketModel message)
        {
            switch (message.command)
            {
                case SecurityAccoutProtocol.GET_CREQ:
                    getSecurityAccountList(token);
                    break;
                case SecurityAccoutProtocol.ADD_CREQ:
                    addSecurityAccount(token, message.GetMessage<string>());
                    break;
                case SecurityAccoutProtocol.UPDATE_CREQ:
                    updateSecurityAccount(token, message.GetMessage<string>());
                    break;
                case SecurityAccoutProtocol.DELETE_CREQ:
                    deleteSecurityAccount(token, message.GetMessage<int[]>());
                    break;
            }
        }

        #region getSecurityAccount
        private void getSecurityAccountList(UserToken token)
        {
            ExecutorPool.Instance.execute(
                     delegate ()
                     {
                         int user_id = ac.getId(token);

                         Console.WriteLine("ID为：" + user_id + "的用户正在进行获取证券账户操作");

                         string strWhere = string.Format("`user_id`={0}", user_id);
                         DataSet ds = sa.GetList(strWhere);
                         List<string> list = new List<string>();
                         foreach (DataRow dr in ds.Tables[0].Rows)
                         {
                             list.Add(dr["id"].ToString() + '+' + dr["security_account"].ToString());
                         }

                         write(token, SecurityAccoutProtocol.GET_SRES, list);
                     }
                     );
        }
        #endregion

        #region addSecurityAccount
        private void addSecurityAccount(UserToken token, string accountName)
        {
            ExecutorPool.Instance.execute(
                     delegate ()
                     {
                         int user_id = ac.getId(token);

                         Console.WriteLine("ID为：" + user_id + "的用户正在进行增加证券账号的操作");

                         sa.security_account = accountName;
                         sa.user_id = user_id;

                         int result;
                         if (sa.Add()) result = sa.GetMaxId();
                         else result = -1;

                         write(token, SecurityAccoutProtocol.ADD_SRES, result);
                     }
                     );
        }
        #endregion

        #region updateSecurityAccount
        private void updateSecurityAccount(UserToken token, string newAccountName)
        {
            ExecutorPool.Instance.execute(
                     delegate ()
                     {
                         int user_id = ac.getId(token);

                         Console.WriteLine("ID为：" + user_id + "用户正在进行更新证券账号操作");

                         string[] s = newAccountName.Split('+');
                         int id = int.Parse(s[0]);

                         sa.id = id;
                         sa.security_account = s[1];
                         bool result = sa.Update();
                         write(token, SecurityAccoutProtocol.UPDATE_SRES, result);
                     }
                     );
        }
        #endregion

        private void deleteSecurityAccount(UserToken token, int[] ids)
        {
            ExecutorPool.Instance.execute(
                     delegate ()
                     {
                         int user_id = ac.getId(token);

                         Console.WriteLine("ID为：" + user_id + "的用户正在进行删除证券账号的操作，需要删除" + ids.Length + "条账号");

                         int result = 0;
                         foreach (int id in ids)
                         {
                             if (sa.Delete(id))
                                 result++;
                         }

                         Console.WriteLine("已完成" + result + "账号的删除");

                         write(token, SecurityAccoutProtocol.DELETE_SRES, result);
                     }
                     );
        }

        public override byte GetType()
        {
            return Protocol.TYPE_SECURITYACCOUNT;
        }
    }
}
